//
//  WCNTabBarController.m
//  WCN
//
//  Created by 杨鑫 on 15/5/29.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "BTTabBarVC.h"
#import "WSJTabarStaticData.h"
#import "SQTabbarControllerAnimatedTransitioning.h"
#import "BTSchoolOfBusinessVC.h"
#import "BTScoolOfMangementVC.h"
#import "BTConsultVC.h"
#import "BTMeVC.h"
#import "BTNavigationContrller.h"
#import "BTLoginVC.h"
#import "UIImage+J.h"
//#import "HELLoginVC.h"

//----------
static NSString *kMessageType = @"MessageType";
static NSString *kConversationChatter = @"ConversationChatter";
static NSString *kGroupName = @"GroupName";
@interface BTTabBarVC () <UITabBarControllerDelegate>

@property(nonatomic, assign) BOOL transiting;

@property (strong, nonatomic) NSDate *lastPlaySoundDate;


@end

@implementation BTTabBarVC

+ (void)load {
    
    // 对item 标题的大小 颜色 设置
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:[self class], nil];
    [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:11],NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
    
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor} forState:UIControlStateSelected];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.tabBar.translucent = NO;
//    // 判断系统版本是否支持 8.0
//    UIView *blurEffectView;
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
//        // 磨砂效果
//        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//        // 磨砂视图
//        blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//    }
//    
//    [self.tabBar insertSubview:blurEffectView atIndex:0];
//    blurEffectView.frame = self.tabBar.bounds;
    [self setupChilds];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:FailureToGetSessionIDNotification object:nil];
    
    //更改tabbar分割线颜色
   // [self.tabBar setShadowImage:[UIImage j_imageWithColor:kMainColor withFrame:CGRectMake(0, 0, kScreenWidth, .5)]];
    //[self.tabBar setBackgroundImage:[[UIImage alloc]init]];
    //如果是登陆状态 初试话数据库
    if (BT_USERID) {
         [[RLMCourseManager  manager] configWithUserAccout:BT_USERID];
    }
    
   
}

- (void)logout {
    
    self.selectedIndex = 0;
    BTNavigationContrller *nav = self.viewControllers[0];
    [nav popToRootViewControllerAnimated:NO];
    BTNavigationContrller *loginNav = [[BTNavigationContrller alloc] initWithRootViewController:[BTLoginVC new]];
    [self presentViewController:loginNav animated:YES
                     completion:nil];
}



- (void)setupChilds {


    BTSchoolOfBusinessVC *sobVC =[BTSchoolOfBusinessVC new];
    BTScoolOfMangementVC *somVC = [BTScoolOfMangementVC new];
    BTConsultVC *consultVC = [BTConsultVC new];
    BTMeVC *meVC = [BTMeVC new];
    
    BTNavigationContrller *businessNav =[[BTNavigationContrller alloc] initWithRootViewController:sobVC];
    BTNavigationContrller *managementNav =[[BTNavigationContrller alloc] initWithRootViewController:somVC];
    BTNavigationContrller *consultNav =[[BTNavigationContrller alloc] initWithRootViewController:consultVC];
    BTNavigationContrller *meNav =[[BTNavigationContrller alloc] initWithRootViewController:meVC];
  
    
  
    
    businessNav.navigationBar.barTintColor = [UIColor redColor];
    businessNav.tabBarItem.title = @"商学院";
    businessNav.tabBarItem.image = [[UIImage imageNamed:@"商学院灰"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ;
    businessNav.tabBarItem.selectedImage =[[UIImage imageNamed:@"商学院"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ;
    
   
    managementNav.tabBarItem.title = @"管理学院";
    managementNav.tabBarItem.image =  [UIImage imageNamed:@"管理学院灰"];
    managementNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"管理学院"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
  
    consultNav.tabBarItem.title = @"资讯";
    consultNav.tabBarItem.image =  [UIImage imageNamed:@"资讯灰"];
    consultNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"资讯蓝"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
//    meNav.view.backgroundColor = [UIColor orangeColor];
    meNav.tabBarItem.title = @"我的";
    meNav.tabBarItem.image =  [UIImage imageNamed:@"我的灰"];
    meNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"我的蓝"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    meNav.navigationBar.hidden = YES;

    self.viewControllers = @[businessNav, managementNav, consultNav, meNav];
  
  
}


#pragma mark - Setter

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    [self tabBarController:self didSelectViewController:self.viewControllers[selectedIndex]];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (_transiting) { return; } _transiting = YES;
    CATransition* animation = [CATransition animation];
    animation.delegate = self;
    [animation setDuration:0.5f];
    [animation setType:kCATransitionFade];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    [[self.view layer]addAnimation:animation forKey:@"switchView"];


}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    _transiting = NO;
}
#pragma mark - UITabBarControllerDelegate
// 当前控制的导航栏项元素
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.navigationItem.titleView = viewController.navigationItem.titleView;
    self.navigationItem.leftBarButtonItems = viewController.navigationItem.leftBarButtonItems;
    self.navigationItem.rightBarButtonItems = viewController.navigationItem.rightBarButtonItems;
}
- (void)showBadageOnItem:(NSInteger)index
{
    
    UITabBarItem *item =  self.tabBar.items[index];
    item.badgeValue  =@"new";
    
}
//导航控制器内的所有vc 只支持竖屏

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if (isIOS8) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait ;
}



@end
